package com.cater.common;

import java.util.List;

import org.apache.shiro.SecurityUtils;

import com.cater.sys.entity.Dep;
import com.cater.sys.entity.Role;
import com.cater.sys.entity.User;

public class UserContext {

	public static final String DEP_IN_SESSION = "dep_in_session";// 登陆用户部门信息
	public static final String DEPPID_IN_SESSION = "deppid_in_session";// 登陆用户公司ID
	public static final String DEPFLOG_IN_SESSION = "depflog_in_session"; // 是否公司直属用户（false 为部门用户）
	public static final String ROLE_IN_SESSION = "role_in_session"; // 角色信息
	public static final String USER_IN_SESSION = "user_in_session"; // 用户信息
	public static final String USER_IN_SESSION_ID = "user_in_session_id"; // 用户ID
	public static final String ISADMIN = "isadmin"; // 是否超级管理员
	public static final String ISADMINTYPE = "isadminType"; // 角色类型
	public static final String COMPANY_ALLUSER_ID = "company_alluser_id";// 用户所属公司所有用户Id
	public static final String DATA_ROLE = "data_role";// 数据权限0全部，1公司，2个人

	public static User get() {
		return (User) SecurityUtils.getSubject().getSession().getAttribute(USER_IN_SESSION);
	}

	public static Dep getDep() {
		return (Dep) SecurityUtils.getSubject().getSession().getAttribute(DEP_IN_SESSION);
	}

	/**
	 * @author sd 获取是否管理员
	 * @return
	 */
	public static Boolean isadmin() {
		return (Boolean) SecurityUtils.getSubject().getSession().getAttribute(ISADMIN);
	}

	/**
	 * @author sd 存储是否是管理员
	 * @param flog
	 */
	public static void setIsadmin(Boolean flog) {
		if (flog != null) {
			SecurityUtils.getSubject().getSession().setAttribute(ISADMIN, flog);
		}
	}

	public static Boolean getDepflog() {
		return (Boolean) SecurityUtils.getSubject().getSession().getAttribute(DEPFLOG_IN_SESSION);
	}

	public static Integer getId() {
		return (Integer) SecurityUtils.getSubject().getSession().getAttribute(USER_IN_SESSION_ID);
	}

	public static Integer getdeppId() {
		return (Integer) SecurityUtils.getSubject().getSession().getAttribute(DEPPID_IN_SESSION);
	}

	@SuppressWarnings("unchecked")
	public static List<Role> getRole() {
		return (List<Role>) SecurityUtils.getSubject().getSession().getAttribute(ROLE_IN_SESSION);
	}

	public static void setRole(List<Role> role) {
		if (role != null) {
			SecurityUtils.getSubject().getSession().setAttribute(ROLE_IN_SESSION, role);
		}
	}

	public static void set(User user) {
		if (user != null) {
			SecurityUtils.getSubject().getSession().setAttribute(USER_IN_SESSION, user);
		}
	}

	public static void setDep(Dep dep) {
		if (dep != null) {
			SecurityUtils.getSubject().getSession().setAttribute(DEP_IN_SESSION, dep);
		}
	}

	public static void setDepflog(Boolean flog) {
		if (flog != null) {
			SecurityUtils.getSubject().getSession().setAttribute(DEPFLOG_IN_SESSION, flog);
		}
	}

	public static void putId(Integer id) {
		SecurityUtils.getSubject().getSession().setAttribute(USER_IN_SESSION_ID, id);
	}

	public static void putdeppId(Integer deppid) {
		SecurityUtils.getSubject().getSession().setAttribute(DEPPID_IN_SESSION, deppid);
	}

	public static void remove() {
		SecurityUtils.getSubject().getSession().removeAttribute(USER_IN_SESSION);
	}

	/**
	 * @author sd 存储管理员类型
	 * @param typeid
	 */
	public static void setIsadminType(Integer typeid) {
		SecurityUtils.getSubject().getSession().setAttribute(ISADMINTYPE, typeid);
	}

	/**
	 * @author sd 获取管理员类型
	 * @return
	 */
	public static Integer getIsadminType() {
		return (Integer) SecurityUtils.getSubject().getSession().getAttribute(ISADMINTYPE);
	}

	/**
	 * @author sd 存储登陆用户能看数据的所有用户ID
	 * @param allUserid
	 */
	public static void setCompanyAllUserId(String allUserid) {
		SecurityUtils.getSubject().getSession().setAttribute(COMPANY_ALLUSER_ID, allUserid);
	}

	/**
	 * @author sd 读取登陆用户能看数据的所有用户ID
	 * @param allUserid
	 */
	public static String getCompanyAllUserid() {
		return (String) SecurityUtils.getSubject().getSession().getAttribute(COMPANY_ALLUSER_ID);
	}

	public static Integer getDataRole() {
		return (Integer) SecurityUtils.getSubject().getSession().getAttribute(DATA_ROLE);
	}

	public static void setDataRole(Integer dataRole) {
		SecurityUtils.getSubject().getSession().setAttribute(DATA_ROLE, dataRole);
	}
}
